package com.fenghuajueli.module_user.utils;

import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

/**
 * AES加密相关工具
 */
public class AesUtils {

    public static final String MODE_PKCS5 = "AES/ECB/PKCS5Padding";

    /**
     * aes对数据进行加密
     *
     * @param data  需要加密的字符串
     * @param appid 当前项目的application_id
     * @return
     * @throws Exception
     */
    public static String encrypt(String data, String appid) {
        if (appid == null) {
            return null;
        }
        try {
            if (appid.length() < 16) {
                int count = 16 - appid.length() % 16;
                StringBuffer stringBuffer = new StringBuffer(appid);
                //这里采用的是ZeroPadding  所有补位填充0
                for (int i = 0; i < count; i++) {
                    stringBuffer.append(0);
                }
                appid = stringBuffer.toString();
            } else if (appid.length() > 16) {
                appid = appid.substring(appid.length() - 16);
            }
            byte[] raw = appid.getBytes("utf-8");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance(MODE_PKCS5);//"算法/模式/补码方式"cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
            byte[] encrypted = cipher.doFinal(data.getBytes("utf-8"));
            return new String(Base64.encodeBase64(encrypted));//此处使用BASE64做转码功能，同时能起到2次加密的作用。
        } catch (Exception e) {
            return null;
        }
    }


    // 解密

    /**
     * aes对数据进行解密
     *
     * @param data  需要加密的字符串
     * @param appid 当前项目的application_id
     * @return
     * @throws Exception
     */
    public static String decrypt(String data, String appid) {
        try {
            // 判断Key是否正确
            if (appid == null) {
                return null;
            }
            if (appid.length() < 16) {
                int count = 16 - appid.length() % 16;
                StringBuffer stringBuffer = new StringBuffer(appid);
                //这里采用的是ZeroPadding  所有补位填充0
                for (int i = 0; i < count; i++) {
                    stringBuffer.append(0);
                }
                appid = stringBuffer.toString();
            } else if (appid.length() > 16) {
                appid = appid.substring(appid.length() - 16);
            }
            byte[] raw = appid.getBytes("utf-8");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance(MODE_PKCS5);
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
            byte[] encrypted1 = Base64.decodeBase64(data.getBytes());//先用base64解密
            try {
                byte[] original = cipher.doFinal(encrypted1);
                String originalString = new String(original, "utf-8");
                return originalString;
            } catch (Exception e) {
                return null;
            }
        } catch (Exception ex) {
            return null;
        }
    }


}
